//
//  NEADViewController.m
//  NetEasyNews
//
//  Created by weiwang on 16/7/17.
//  Copyright © 2016年 王伟. All rights reserved.
//

#import "NEADViewController.h"
#import "NECircularProgressView.h"
#import "NENetWorkManager.h"
#import "NEADEntity.h"
#import "NEADDataEntity.h"
#import <MJExtension/MJExtension.h>
#import "NEADViewModel.h"
#import "NEDBManager.h"

#define kCachedNewImage     ([[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingString:@"/adLaunchImg.png"])

@interface NEADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *adImageView;
@property (weak, nonatomic) IBOutlet UIImageView *adBottom;
@property (weak, nonatomic) IBOutlet NECircularProgressView *circlaur;
@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) NEADViewModel *adViewModel;
@end

@implementation NEADViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    self.adImageView.image = [UIImage imageWithContentsOfFile:kCachedNewImage];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(_upadateProgress) userInfo:nil repeats:YES];
    
    self.circlaur.progressChangedBlock = ^(NECircularProgressView *pv, CGFloat progress){
        if (progress <= 0.0) {
            [self.timer invalidate];
            [UIView animateWithDuration:1.f
                                  delay:0.f
                                options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationCurveEaseIn
                             animations:^
             {
                 CGAffineTransform scaleTransform = CGAffineTransformMakeScale(2.5, 2.5);
                 self.view.transform = scaleTransform;
                 self.view.alpha = 0.f;
             }
             completion:^(BOOL finished)
            {
                 if (finished) {
                     [self.view removeFromSuperview];
                     [self removeFromParentViewController];
                 }
             }];
        }
    };
    
    @weakify(self)
    [[self.adViewModel.fetchADCommand execute:nil] subscribeNext:^(id x){
       @strongify(self)
        NEADEntity *entity = (NEADEntity *)x;
        
        int value = (arc4random() % entity.ads.count);
        NEADDataEntity *dataEntity = entity.ads[value];
        [self _downloadImage:dataEntity.res_url.firstObject];
    }error:^(NSError *error) {
        NSLog(@"failure %@",error.userInfo);
    }];

}

#pragma setter and getter
- (NEADViewModel *)adViewModel
{
    if (_adViewModel == nil) {
        _adViewModel = [[NEADViewModel alloc]init];
    }
    return _adViewModel;
}

#pragma private method
-(void)_downloadImage:(NSString *)imageUrl
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:imageUrl]];
    [[[NENetWorkManager sharedInstance] downloadTaskWithRequest:request progress:nil destination: ^NSURL *(NSURL *targetPath, NSURLResponse *response){
        [[NSFileManager defaultManager] removeItemAtPath:kCachedNewImage error:nil];
        return [NSURL fileURLWithPath:kCachedNewImage];
    } completionHandler:nil] resume];
    
   // [[NEDBManager shareInstance] instertURLS];
}

-(void)_upadateProgress
{
    self.circlaur.progress = self.circlaur.progress - 0.035f/3.f;
}


@end
